package com.sg.service.base.framework.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.dto.base.framework.req.BatchQueryCheckItemStatListTestDataReq;
import com.sg.dto.base.framework.req.DeleteCheckItemStatReq;
import com.sg.dto.base.framework.req.QueryCheckItemStatDetailReq;
import com.sg.dto.base.framework.req.QueryCheckItemStatReq;
import com.sg.entity.StatusCheckItem;
import com.sg.service.base.framework.MStatusCheckItemService;
import com.wicket.okrframework.common.dto.FieldInfo;
import com.wicket.okrframework.common.exception.BizException;
import com.wicket.okrframework.dal.mapper.mbg.StatusCheckItemMapper;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author ：
 * @version 1.0
 * @since 2022/1/19 15:39
 */
@Service
public class MStatusCheckItemServiceImpl
        extends ServiceImpl<StatusCheckItemMapper, StatusCheckItem>
        implements MStatusCheckItemService {

    @Autowired
    StatusCheckItemMapper statusCheckItemMapper;

    /**
     * 1-1-22批量新增状态校验项[951]
     * gen by moon at 11/28/2022, 2:15:57 PM
     */
    @Trace(operationName = "1-1-22批量新增状态校验项")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchAddCheckItemStat(List<StatusCheckItem> statusCheckItem) {

        if (statusCheckItem == null || statusCheckItem.size() == 0) {
            return false;
        }

        return super.saveBatch(statusCheckItem.stream().map(item -> {
            item.setStatusCheckItemId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
            item.setIsComplete("FALSE");
            item.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
            item.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
            item.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
            item.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
            return item;
        }).collect(Collectors.toList()));
    }

    /**
     * 1-1-22查询状态校验项列表[328]
     * gen by moon at 10/16/2022, 11:09:01 AM
     */
    @Trace(operationName = "1-1-22查询状态校验项列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<StatusCheckItem> queryCheckItemStat(QueryCheckItemStatReq reqDto) {

        try {
            QueryWrapper<StatusCheckItem> queryWrapper = new QueryWrapper<>();


            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(StatusCheckItem.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(StatusCheckItem.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(StatusCheckItem.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<StatusCheckItem>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 1-1-22查询状态校验项详情[327]
     * gen by moon at 10/16/2022, 11:09:01 AM
     */
    @Trace(operationName = "1-1-22查询状态校验项详情")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public StatusCheckItem queryCheckItemStatDetail(QueryCheckItemStatDetailReq reqDto) {

        try {

            QueryWrapper<StatusCheckItem> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<StatusCheckItem> retList = super.list(queryWrapper);
            if (retList != null && retList.size() > 0) {
                StatusCheckItem retData = retList.get(0);

                return retData;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 1-1-22删除状态校验项[326]
     * gen by moon at 10/16/2022, 11:09:00 AM
     */
    @Trace(operationName = "1-1-22删除状态校验项")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteCheckItemStat(DeleteCheckItemStatReq reqDto) {

        try {
            Map<String, Object> columnMap = CommonFunctionHelper.objectToMap(reqDto);
            return super.removeByMap(columnMap);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 1-1-22修改状态校验项	[325]
     * gen by moon at 10/16/2022, 11:08:59 AM
     */
    @Trace(operationName = "1-1-22修改状态校验项	")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateCheckItemStat(StatusCheckItem statusCheckItem) {
        statusCheckItem.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        statusCheckItem.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        return super.updateById(statusCheckItem);
    }

    /**
     * 1-1-22新增状态校验项[324]
     * gen by moon at 10/16/2022, 11:08:59 AM
     */
    @Trace(operationName = "1-1-22新增状态校验项")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addCheckItemStat(StatusCheckItem statusCheckItem) {
        statusCheckItem.setStatusCheckItemId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
        statusCheckItem.setCheckItemCode("CI");//CUSTOM_CONVENTION
        statusCheckItem.setIsComplete("FALSE");
        statusCheckItem.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        statusCheckItem.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        statusCheckItem.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        statusCheckItem.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        statusCheckItem.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
        statusCheckItem.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
        if (super.save(statusCheckItem)) {
            return statusCheckItem.getStatusCheckItemId();
        } else {
            return "-1";
        }

    }

    /**
     * M1批量查询组织的状态校验项测试[4878]
     * gen by moon at 10/16/2022, 11:09:02 AM
     */
    @Trace(operationName = "M1批量查询组织的状态校验项测试")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<StatusCheckItem> batchQueryCheckItemStatListTestData(BatchQueryCheckItemStatListTestDataReq reqDto) {

        try {
            QueryWrapper<StatusCheckItem> queryWrapper = new QueryWrapper<>();

            if (reqDto.getCheckItemStatListTestDataList() == null || reqDto.getCheckItemStatListTestDataList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("entity_id", reqDto.getCheckItemStatListTestDataList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(StatusCheckItem.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(StatusCheckItem.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(StatusCheckItem.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<StatusCheckItem>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * M1批量删组织的状态校验项测试[4879]
     * gen by moon at 10/16/2022, 11:09:03 AM
     */
    @Trace(operationName = "M1批量删组织的状态校验项测试")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchDeleteCheckItemStatListTestData(List<String> reqDto) {

        if (reqDto == null || reqDto.size() == 0) {
            return false;
        }

        return super.removeByIds(reqDto);
    }
}
